package com.levi.async;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

/**
 * @author shentong
 * @since 2022/1/13 8:16 PM
 */
public class FutureDemo {
    static Random random;
    static ExecutorService executorService = Executors.newFixedThreadPool(10);

    public static Integer getAticleScoreSum(String articleName){
        List<Future<Integer>> futures = new ArrayList<>();
        Future<Integer> futureA = executorService.submit(new CalculateArticleScoreA());
        Future<Integer> futureB = executorService.submit(new CalculateArticleScoreB());
        Future<Integer> futureC = executorService.submit(new CalculateArticleScoreC());
        System.out.println("Main Thread is running~");
        //一个一个获取future的结果
//        Integer a = null;
//        try {
//            a = futureA.get(3,TimeUnit.SECONDS);
//        } catch (InterruptedException | ExecutionException | TimeoutException e) {
//            futureA.cancel(true);
//            e.printStackTrace();
//        }
//        Integer b = null;
//        try {
//            b = futureB.get(3,TimeUnit.SECONDS);
//        } catch (InterruptedException | ExecutionException | TimeoutException e) {
//            futureA.cancel(true);
//            e.printStackTrace();
//        }
//        Integer c = null;
//        try {
//            c = futureC.get(3,TimeUnit.SECONDS);
//        } catch (InterruptedException | ExecutionException | TimeoutException e) {
//            futureA.cancel(true);
//            e.printStackTrace();
//        }
//        executorService.shutdown();
//        return a + b + c;
        futures.add(futureA);
        futures.add(futureB);
        futures.add(futureC);
        int sum = 0;
        //for循环获取每个future的结果
        for (Future<Integer> future : futures) {
            try {
                sum += future.get(10, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                future.cancel(true);
                e.printStackTrace();
            }
        }
        executorService.shutdown();
        return sum;
    }

    private static class CalculateArticleScoreA implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            TimeUnit.SECONDS.sleep(2);
            System.out.println(Thread.currentThread().getName());
            return 1;
        }
    }

    private static class CalculateArticleScoreB implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            TimeUnit.SECONDS.sleep(4);
            System.out.println(Thread.currentThread().getName());
            return 2;
        }
    }

    private static class CalculateArticleScoreC implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            TimeUnit.SECONDS.sleep(6);
            System.out.println(Thread.currentThread().getName());
            return 3;
        }
    }

    public static void main(String[] args) {
        System.out.println(getAticleScoreSum(""));
    }
}
